/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.dhecp.project.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.layui.TreeDataModel;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.base.mapper.BaseInfoMapper;
import com.dhecp.project.base.service.BaseInfoService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;

/**
 * 
 * @author DH-Xxj
 * @version 2019年8月27日
 */

@Service
public class BaseInfoServiceImpl implements BaseInfoService {

	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	/**通过guid查询基础数据
	 * @return BaseInfo
	 */
	@Override
	public BaseInfo queryBaseInfoByGuid(String guid) {
		return baseInfoMapper.findBaseInfoByGuid(guid);
	}

	/**通过基础数据分项（vtype）、基础数据编码（vid）查询基础数据
	 * @return BaseInfo
	 */
	@Override
	public BaseInfo queryBaseInfoByVtypeAndVid(String vtype, String vid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("vtype", vtype);
		params.put("vid", vid);
		return baseInfoMapper.findBaseInfoByVtypeAndVid(params);
	}

	/**
	 * 通过传入的条件查询基础数据列表
	 * @param 若存在键"nowherestr"（任意值），表示不进行条件过滤
	 * @return List-BaseInfo
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public List<BaseInfo> queryBaseInfoList(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句
		if (params.get("vtype") != null && "bank".equals(params.get("vtype").toString().toLowerCase())){
	        //银行信息单独查询，需要关联公司信息表
			PageUtils.startPage();
			return baseInfoMapper.findBaseInfoBankList(params);
		}
        PageUtils.startPage();
		return baseInfoMapper.findBaseInfoList(params);
	}

	/**
	 * 通过传入的条件查询基础数据列表(树形列表数据)
	 * @param 若存在键"nowherestr"（任意值），表示不进行条件过滤
	 * @return List
	 * @author DH-Xxj
	 * @throws Exception 
	 */
	@Override
	public List<TreeDataModel> queryBaseInfoTree(Map<String, Object> params) throws Exception {
		params.put("moduleTable", "TB_baseinfo");
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));		//常规查询条件SQL语句		
		return baseInfoMapper.findBaseInfoTree(params);
	}

	/**新增基础数据
	 * @return MessageModel
	 */
	@Override
	public AjaxResult insertBaseInfo(String baseInfoItems) {
		if (StringUtils.isEmpty(baseInfoItems)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<BaseInfo> baseInfoList = JSON.parseArray(baseInfoItems, BaseInfo.class);
		if (baseInfoList == null || baseInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (BaseInfo baseInfo : baseInfoList) {
			if (baseInfo.getVid() == null || "".equals(baseInfo.getVid().toString())){
				baseInfo.setVid(baseInfo.getVname());
			}
			baseInfoMapper.insert(baseInfo);
		}
		return AjaxResult.success("操作成功！");
	}

	/**删除基础数据
	 * @return MessageModel
	 */
	@Override
	public AjaxResult deleteBaseInfo(String baseInfoItems) {
		if (StringUtils.isEmpty(baseInfoItems)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<BaseInfo> baseInfoList = JSON.parseArray(baseInfoItems, BaseInfo.class);
		if (baseInfoList == null || baseInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (BaseInfo baseInfo : baseInfoList) {
			baseInfoMapper.deleteByGuid(baseInfo);
		}
		return AjaxResult.success("操作成功！");
	}

	/**修改基础数据
	 * @return MessageModel
	 */
	@Override
	public AjaxResult updateBaseInfo(String baseInfoItems) {
		if (StringUtils.isEmpty(baseInfoItems)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<BaseInfo> baseInfoList = JSON.parseArray(baseInfoItems, BaseInfo.class);
		if (baseInfoList == null || baseInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (BaseInfo baseInfo : baseInfoList) {
			String vid = baseInfoMapper.checkedVidHidden(baseInfo.getVtype().toString());
			if (vid != null){
				baseInfo.setVid(baseInfo.getVname());
			}
			baseInfoMapper.update(baseInfo);
		}
		return AjaxResult.success("操作成功！");
	}

	/**修改基础数据默认标志
	 * @return MessageModel
	 */
	@Override
	public AjaxResult updateBaseInfoBdefault(String baseInfoItems) {
		if (StringUtils.isEmpty(baseInfoItems)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<BaseInfo> baseInfoList = JSON.parseArray(baseInfoItems, BaseInfo.class);
		if (baseInfoList == null || baseInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (BaseInfo baseInfo : baseInfoList) {
			baseInfoMapper.updateBaseInfoBdefault(baseInfo);
		}
		return AjaxResult.success("操作成功！");
	}

	/**修改基础数据禁用标志
	 * @return MessageModel
	 */
	@Override
	public AjaxResult updateBaseInfoBdeleted(String baseInfoItems) {
		if (StringUtils.isEmpty(baseInfoItems)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<BaseInfo> baseInfoList = JSON.parseArray(baseInfoItems, BaseInfo.class);
		if (baseInfoList == null || baseInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (BaseInfo baseInfo : baseInfoList) {
			baseInfoMapper.updateBaseInfoBdeleted(baseInfo);
		}
		return AjaxResult.success("操作成功！");
	}

}
